<?php
if(isset($_POST['dosetup'])) {
	header('content-type:text/html; charset=utf-8');
	include '../wogu/App.php';
	
	App::getInstance()->init()->initDb();
	$config = Db_Factory::getConfig();
	$tablepre = $config['default']['tablepre'];
	$db = Db_Factory::getDatabase();
	$data = file_get_contents('wa.sql');
	$sqls = explode(';', $data);
	foreach($sqls as $sql) {
		if(!empty($sql)) {
			$sql = str_replace('%TABLEPRE%', $tablepre, $sql);
			$db->query($sql);
		}
	}
	
	$salt = random(6);
	$password = password($_POST['password'], $salt);
	$setarr = array(
		'groupid' => 1,
		'username' => $_POST['username'],
		'password' => $password,
		'salt' => $salt,
		'force_qq' => 0,
		'isfree' => 1
	);
	$db->insert($setarr, $tablepre.'admin');
	
	//删除缓存
	foreach (glob('../wogu/data/cache/*') as $filename) {
	    @unlink($filename);
	}
	
	echo '安装成功，请删除install目录！<a href="'.geturl('admin.php').'">登陆后台</a>';
} else {
?>
<!DOCTYPE html>
<html>
<head>
	<title>安装 - WoguAdmin - power by wogu</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>###################################################################</p>
<p>1.安装前请先配置/wogu/configs/database.inc.php</p>
<p>2.安装完成后可继续配置/wogu/configs/下的文件</p>
<p>3.安装完成后请删除install目录</p>
<p>###################################################################</p>
<form method="post" action="index.php">
	<table>
		<caption>创建创始人帐号</caption>
		<tr>
			<td>用户名</td>
			<td><input type="text" name="username"></td>
		</tr>
		<tr>
			<td>密码</td>
			<td><input type="text" name="password"></td>
		</tr>
		<tr>
			<td><input type="submit" name="dosetup" value="安装"></td>
			<td></td>
		</tr>
	</table>
</form>
</body>
</html>
<?php } ?>